//
// Main.cs
//
// Authors:
//   Gregor Burger burger.gregor@gmail.com
//
// Copyright (C) 2006 Gregor Burger
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Diagnostics;

namespace SampleTracker
{
    class Program
    {
        public void OnProcessExit(object sender, EventArgs e)
        {
            //Console.Write("shutting down the Tracker...");
            //TrackerEngine.Instance.Stop();
            //Console.WriteLine("done");
        }

        public static void Main(string[] args)
        {
            Debug.Listeners.Add(new TextWriterTraceListener(Console.Out));

            Console.WriteLine("Welcome to the MonoTorrent tracker");
            Console.WriteLine("1. Start the tracker");
            Console.WriteLine("2. Start a benchmark");

            Console.Write("Choice: ");

            int val = 0;
            while (val != 1 && val != 2)
                val = GetInt();

            if (val == 1)
                StartTracker();
            else
                Benchmark();
        }

        private static void Benchmark()
        {
            Console.Clear();
            Console.Write("How many active torrents will be simulated: ");
            int torrents = GetInt();
            Console.Write("How many active peers per torrent: ");
            int peers = GetInt();
            Console.Write("How many requests per second: ");
            int requests = GetInt();

            Console.Write("What is the tracker address: ");
            string address = Console.ReadLine();

            StressTest test = new StressTest(torrents, peers, requests);
            test.Start(address);

            while (true)
            {
                Console.WriteLine("Measured announces/sec:  {0}", test.RequestRate);
                Console.WriteLine("Total announces: {0}", test.TotalTrackerRequests);
                Console.WriteLine(Environment.NewLine);
                System.Threading.Thread.Sleep(1000);
            }
        }

        private static void StartTracker()
        {
            new SimpleTracker();
        }

        private static int GetInt()
        {
            int ret = 0;
            while (!int.TryParse(Console.ReadLine(), out ret))
            {
            }
            return ret;
        }
    }
}